package com.qing.expert.modules.admin.service.impl;

import com.qing.expert.common.exception.BusinessException;
import com.qing.expert.modules.admin.service.AdminUserService;
import com.qing.expert.modules.user.entity.User;
import com.qing.expert.modules.user.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;

@Slf4j
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserStatus(String userId, Integer status) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }

        // 更新用户状态
        user.setStatus(status);
        int rows = userMapper.updateById(user);
        if (rows <= 0) {
            throw new BusinessException("更新用户状态失败");
        }
        log.info("管理员更新用户[{}]状态为[{}]", userId, status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserRole(String userId, Integer role) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }

        // 更新用户角色
        user.setRole(role);
        int rows = userMapper.updateById(user);
        if (rows <= 0) {
            throw new BusinessException("更新用户角色失败");
        }
        log.info("管理员更新用户[{}]角色为[{}]", userId, role);
    }
}